package forms;

import java.awt.BorderLayout;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import src.Evidence;
import src.SouborIO;
import src.Zak;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 28.4.2011, 17:27:57
 */

/**
 *
 * @author Viktor
 */
//
public class MainFrame extends javax.swing.JFrame {
    int mesic;
    Date datum;
    MyTimePanel mtp = null;
    ViewPanel vp = null;
    Evidence e = null;
    Zak zobrZak = null;
    InfoPanel ip = null;
     Calendar cal = null; 
     SouborIO io = null;
     int den ;
     boolean deleting;
 
//    DateFormat fDateFormat = null;
    

  
    /** Creates new form MainFrame */
    public MainFrame() {
        io = new SouborIO();
        initComponents();
        deleting = false;
        cal = new GregorianCalendar();
        mesic = cal.get(Calendar.MONTH);
        den = cal.get(Calendar.DATE);

        
        setSize(1000,700);
        setTitle("Evidence");
        e = io.nacist("evidence.bin");
        if(e==null){
             e = new Evidence();
             io.ulozit("evidence.bin", e);
        }
        
      
      //  testZak();
        //testZak2();
        mtp = new MyTimePanel(e);
         ip = new InfoPanel(zobrZak,mesic);
        vp = new ViewPanel(e,zobrZak,ip,mesic,den);
       
       this.add(mtp) ;
       this.add(vp);
       this.add(ip);
//       this.setIconImage(new ImageIcon("icon.png").getImage());
//      this.setIconImage(Toolkit.getDefaultToolkit().getImage("iconsmall.gif"));
//        add(mtp,BoxLayout.Y_AXIS);
//        fDateFormat = DateFormat.getTimeInstance(DateFormat.DEFAULT);
//        Date ted = new Date();
//        String date_out = fDateFormat.format (ted);
      // nactiPoSpusteni()
        
//      vp.setEvidence(e);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        jPanel2 = new javax.swing.JPanel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        jToolBar1.setRollover(true);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 571, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(43, Short.MAX_VALUE)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 591, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 284, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setIconImages(null);
        getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.Y_AXIS));

        jMenu2.setText("Úpravy");

        jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/user_add_32.png"))); // NOI18N
        jMenuItem2.setText("Vložit žáka");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        jMenuItem6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/user_close_32.png"))); // NOI18N
        jMenuItem6.setText("Odstranit žáka");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem6);

        jMenuItem7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/user_blue_32.png"))); // NOI18N
        jMenuItem7.setText("Upravit žáka");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem7);

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_W, java.awt.event.InputEvent.CTRL_MASK));
        jMenuItem3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pictures/clock_32.png"))); // NOI18N
        jMenuItem3.setText("Vložit Kroužek");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem3);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        // TODO add your handling code here:
        ZakEdit ze = new ZakEdit(e,vp,true,null);
                ze.setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
        KrouzekEdit ke = new KrouzekEdit(e);
                    ke.setVisible(true);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        // TODO add your handling code here:
        /*odstranění žáka*/
        if(ip.getZobrZak() != null){
           boolean vysl = e.getZaci().remove(ip.getZobrZak());
            vp.update();
            io.ulozit("evidence.bin", e);
           if(!vysl){
               JOptionPane.showMessageDialog(rootPane,"Chyba při odstranění žáka - žák neexistuje");
         
           }
        }else{
            JOptionPane.showMessageDialog(rootPane, "Neni vybrán žádný žák");
        }
  
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
        ZakEdit ze = new ZakEdit(e, vp,false, ip.getZobrZak());
        ze.setVisible(true);
        
    }//GEN-LAST:event_jMenuItem7ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new MainFrame().setVisible(true);
               
               MainFrame mf =  new MainFrame();
               mf.addWindowListener(new WindowAdapter() {

                    @Override
                    public void windowClosing(WindowEvent e) {
                        super.windowClosing(e);
                       int reply = JOptionPane.showConfirmDialog(null, "Přejete si databázi žáků uložit ?", "Ukončení aplikace", JOptionPane.YES_NO_CANCEL_OPTION);
                       switch(reply){
                           case JOptionPane.YES_OPTION :
                               System.out.println("Ulozi");
                               System.exit(0);
                               break;
                           case JOptionPane.NO_OPTION :
                               System.out.println("Neulozi");
                                System.exit(0);
                               break;    
                           case JOptionPane.CANCEL_OPTION :
                               System.out.println("Zrusi");
                               break;    
                       }
                    }
               
               });
               mf.setIconImage(Toolkit.getDefaultToolkit().getImage("iconsmall.png"));
               mf.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables

    
//   private void testZak(){
//       Zak z = new Zak("Novák", "Jan","772001002", "12.12.1990", "Třída Míru 92, Pardubice");
//      e.pridejDoEvidence(z);
//   }
//   private void testZak2(){
//       Zak z = new Zak("Novák", "František","772001002", "12.12.1990", "Třída Míru 92, Pardubice");
//      e.pridejDoEvidence(z);
//   }
}
